package dao.impl;

import dao.BaseDao;
import dao.NewsHeadLineDao;
import pojo.NewsHeadline;
import pojo.vo.HeadlineDetailVo;
import pojo.vo.HeadlinePageVo;
import pojo.vo.HeadlineQueryVo;

import java.util.ArrayList;
import java.util.List;

public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao {
    @Override
    public int addNewsHeadline(NewsHeadline newsHeadline) {
        String sql = """
                insert into news_headline 
                values (DEFAULT,?,?,?,?,0,now(),now(),0)
                """;
        return baseUpdate(sql,
                newsHeadline.getTitle(),
                newsHeadline.getArticle(),
                newsHeadline.getType(),
                newsHeadline.getPublisher()
        );
    }

    @Override
    public NewsHeadline findHeadlineByHid(Integer hid) {
        String sql = """
                select
                    hid,
                    title,
                    article,
                    type,
                    publisher,
                    page_views pageViews,
                    create_time createTime,
                    update_time updateTime,
                    is_deleted isDeleted
                from 
                    news_headline
                where 
                    hid = ?
                """;
        List<NewsHeadline> list = baseQuery(NewsHeadline.class, sql, hid);
        return null != list && list.size()>0?list.get(0):null;
    }

    @Override
    public int updateNewsHeadline(NewsHeadline newsHeadline) {
        String sql = """
                update 
                    news_headline
                set
                    title = ?,
                    article = ?,
                    type = ?,
                    update_time = now()
                where 
                    hid = ?     
                """;
        return baseUpdate(sql,
                newsHeadline.getTitle(),
                newsHeadline.getArticle(),
                newsHeadline.getType(),
                newsHeadline.getHid()
        );
    }

    @Override
    public int removeByHid(int hid) {
        String sql ="update news_headline set is_deleted =1 ,  update_time =NOW() where hid = ? ";
        return baseUpdate(sql,hid);
    }

    @Override
    public List<NewsHeadline> findByPublisher(Integer publisherId) {
        String sql = """
        SELECT hid, title, article, type, publisher, page_views pageViews, 
               create_time createTime, update_time updateTime, is_deleted isDeleted
        FROM news_headline
        WHERE publisher = ? AND is_deleted = 0
        ORDER BY create_time DESC
        """;
        return baseQuery(NewsHeadline.class, sql, publisherId);
    }

    @Override
    public List<HeadlinePageVo> findByPublisherPage(Integer publisherId, int start, int pageSize) {
        String sql = """
    SELECT hid, title, type, page_views pageViews,
           TIMESTAMPDIFF(HOUR, create_time, now()) pastHours, publisher
    FROM news_headline
    WHERE publisher = ? AND is_deleted = 0
    ORDER BY create_time DESC
    LIMIT ?, ?
    """;
        return baseQuery(HeadlinePageVo.class, sql, publisherId, start, pageSize);
    }

    @Override
    public int findByPublisherCount(Integer publisherId) {
        String sql = """
    SELECT count(1)
    FROM news_headline
    WHERE publisher = ? AND is_deleted = 0
    """;
        Long count = baseQueryObject(Long.class, sql, publisherId);
        return count.intValue();
    }

    @Override
    public List<HeadlinePageVo> findPageList(HeadlineQueryVo headlineQueryVo) {
        List params = new ArrayList();
        String sql = """
                select 
                    hid ,
                    title,
                    type,
                    page_views pageViews,
                    TIMESTAMPDIFF(HOUR,create_time,now()) pastHours ,
                    publisher 
                from 
                    news_headline
                where 
                    is_deleted = 0
                
                """;
        if (headlineQueryVo.getType() != 0) {
            sql = sql.concat(" and type = ? ");
            params.add(headlineQueryVo.getType());
        }
        if (headlineQueryVo.getKeyWords() != null && !headlineQueryVo.getKeyWords().equals("")) {
            sql = sql.concat(" and title like ? ");
            params.add("%" + headlineQueryVo.getKeyWords() + "%");
        }
        sql = sql.concat(" order by pastHours ASC , page_views DESC ");
        sql = sql.concat(" limit ? , ? ");
        params.add((headlineQueryVo.getPageNum() - 1) * headlineQueryVo.getPageSize());
        params.add(headlineQueryVo.getPageSize());
        return baseQuery(HeadlinePageVo.class, sql, params.toArray());
    }
    @Override
    public int findPageCount(HeadlineQueryVo headlineQueryVo) {
        List params = new ArrayList();

        String sql = """
                select 
                    count(1)
                from 
                    news_headline
                where 
                    is_deleted = 0
                
                """;
        if (headlineQueryVo.getType() != 0) {
            sql = sql.concat(" and type = ? ");
            params.add(headlineQueryVo.getType());
        }
        if (headlineQueryVo.getKeyWords() != null && !headlineQueryVo.getKeyWords().equals("")) {
            sql = sql.concat(" and title like ? ");
            params.add("%" + headlineQueryVo.getKeyWords() + "%");
        }
        Long count = baseQueryObject(Long.class, sql, params.toArray());
        return count.intValue();
    }

    @Override
    public int increasePageViews(int hid) {
        String sql ="update news_headline set page_views = page_views +1 where hid =?";
        return baseUpdate(sql,hid);
    }

    @Override
    public HeadlineDetailVo findHeadlineDetail(int hid) {
        String sql ="select hid,title,article,type, tname typeName ,page_views pageViews,TIMESTAMPDIFF(HOUR,create_time,NOW()) pastHours,publisher,nick_name author from news_headline h left join  news_type t on h.type = t.tid left join news_user u  on h.publisher = u.uid where hid = ?";
        List<HeadlineDetailVo> headlineDetailVoList = baseQuery(HeadlineDetailVo.class, sql, hid);
        if(null != headlineDetailVoList && headlineDetailVoList.size()>0)
            return headlineDetailVoList.get(0);
        return null;
    }
}
